import VxeColumn from './VxeColumn';
import toolButtons from './ToolButtons';
import tableAction from './TableAction';
import { VxeGridInstance, BasicTableProps } from '@/components/VxeTable';
import { ref, reactive } from 'vue';
import tableFilter from '@/components/TableFilter/index.vue';
import { deepClone } from 'ele-admin-pro';
export const TableRef = ref<VxeGridInstance>();
export const FilterRef = ref<InstanceType<typeof tableFilter>>();
export default {
  FilterRef: ref<InstanceType<typeof tableFilter>>()
};

export const useConfig = () => {
  const FilterRef = ref<InstanceType<typeof tableFilter>>();
  const TableRef = ref<VxeGridInstance>();
  return {
    TableRef,
    FilterRef,
    Columns: VxeColumn,
    ToolBtn: toolButtons,
    Action: tableAction,
    TableFilter: tableFilter,
    getFormFields: () => {
      const result = [] as any;
      let index = 0;
      const tempAry = [] as any[];
      for (let i = 0; i < VxeColumn.length; i++) {
        if (VxeColumn[i].field) {
          if (VxeColumn[i].isRow) {
            result.push([VxeColumn[i]]);
          } else {
            if (index == 1) {
              tempAry.push(VxeColumn[i]);
              result.push(deepClone(tempAry));
              index = 0;
              tempAry.length = 0;
            } else {
              tempAry.push(VxeColumn[i]);
              index = index + 1;
            }
          }
        }
      }
      if (tempAry.length > 0) {
        result.push(tempAry);
      }
      return result;
    },
    tableOptions: (queryCall: (param) => Promise<any>) => {
      return reactive<BasicTableProps>({
        border: true,
        height: 'auto',
        columnConfig: {
          resizable: true
        },
        columns: VxeColumn,
        proxyConfig: {
          ajax: {
            query: ({ page, form }) => {
              if (
                FilterRef.value &&
                FilterRef.value.conModelsJson &&
                FilterRef.value.conModelsJson.length
              ) {
                form.ConModels = FilterRef.value?.conModelsJson;
              }

              return queryCall({ ...page, ...form });
            }
          }
        }
      });
    }
  };
};
